<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" :request-api="getTableList" :init-param="initParam">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button type="primary" :icon="Download" plain @click="downloadFile">导出数据</el-button>
      </template>
      <template #name="scope">
        <el-button type="primary" link @click="onView(scope.row)"> {{ scope.row.name }} </el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="primary" link :icon="View" @click="openDrawer('查看', scope.row)">查看</el-button>
        <el-button v-if="BUTTONS.edit" type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)">编辑</el-button>
        <el-button v-if="BUTTONS.delete" type="primary" link :icon="Delete" @click="deleteAccount(scope.row)">删除</el-button>
      </template>
    </ProTable>
    <DetailDrawer ref="drawerRef" />
    <el-dialog v-model="dialogVisible" :destroy-on-close="true" width="90%" top="20px" title="富文本内容预览">
      <div class="view" v-html="content"></div>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="tsx" name="useProTable">
import { ref, reactive } from "vue";
import { ExportObj, Backlog } from "@/api/types/base";
import { useHandleData } from "@/hooks/useHandleData";
import { useDownload } from "@/hooks/useDownload";
import { ElMessageBox } from "element-plus";
import ProTable from "@/components/ProTable/index.vue";
import DetailDrawer from "./detailDrawer.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { Delete, EditPen, Download, View } from "@element-plus/icons-vue";
import apiService from "@/api/services/apiService";
import { useDateFormat } from "@vueuse/core";
import { useAuthButtons } from "@/hooks/useAuthButtons";
import { ExportColType } from "@/enums/sysEnum";
import { dateRender } from "@/hooks/useColumnRender";

const initVar = reactive({
  tbName: "backlog",
  tbCnName: "故事"
});

const dialogVisible = ref(false);
const content = ref("");

// ProTable 实例
const proTable = ref<ProTableInstance>();
const { BUTTONS } = useAuthButtons();

const initParam = reactive({
  condition: {},
  options: { sort: { createDate: -1 } }
});

const getTableList = (params: any) => {
  return apiService.findPage(initVar.tbName, params.condition, params.options);
};

const onView = (row: Backlog) => {
  dialogVisible.value = true;
  content.value = row.how;
};

// 表格配置项
const columns = reactive<ColumnProps<Backlog>[]>([
  { type: "index", label: "序号", width: 85 },
  { prop: "id", label: "标识", width: 80 },
  { prop: "name", label: "摘要", search: { el: "input", like: true } },
  { prop: "product", label: "产品", width: 150, search: { el: "input", like: true } },
  { prop: "module", label: "功能", width: 100, search: { el: "input", like: true } },
  { prop: "sprint", label: "冲刺", width: 220, align: "left", search: { el: "input", like: true } },
  { prop: "estimate", label: "故事点", width: 85 },
  { prop: "createUser", label: "需求", width: 100, search: { el: "input", like: true } },
  { prop: "testUser", label: "测试", width: 100 },
  { prop: "uiUser", label: "UI设计", width: 100 },
  { prop: "createDate", label: "创建时间", width: 120, render: dateRender }
]);

// 删除信息
const deleteAccount = async (params: Backlog) => {
  await useHandleData(apiService.deleteById, initVar.tbName, params._id, `删除【${params.name}】`);
  proTable.value?.getTableList();
};

const getExcelConfig = (): ExportObj => {
  return {
    params: proTable.value?.totalParam,
    tableName: initVar.tbName,
    keyPropName: "ip",
    enumTypes: [],
    tableConfigs: [],
    header: [
      { header: "序号", width: 6, ext: { prop: "index", type: ExportColType.INDEX } },
      { header: "服务名称", width: 16, ext: { prop: "name", type: ExportColType.DEFAULT } },
      { header: "操作系统", width: 16, ext: { prop: "system", type: ExportColType.DEFAULT } },
      { header: "IP地址", width: 10, ext: { prop: "ip", type: ExportColType.DEFAULT } },
      { header: "用途", width: 40, ext: { prop: "use", type: ExportColType.DEFAULT } },
      { header: "CPU(核)", width: 8, ext: { prop: "cpu", type: ExportColType.DEFAULT } },
      { header: "内存(G)", width: 8, ext: { prop: "ram", type: ExportColType.DEFAULT } },
      { header: "硬盘(G)", width: 8, ext: { prop: "hdd", type: ExportColType.DEFAULT } },
      { header: "使用状态", width: 8, ext: { prop: "resouceState", type: ExportColType.DEFAULT } },
      { header: "服务器类型", width: 8, ext: { prop: "address", type: ExportColType.DEFAULT } },
      { header: "负责人", width: 8, ext: { prop: "ownerUser", type: ExportColType.DEFAULT } },
      { header: "使用期限", width: 8, ext: { prop: "useDate", type: ExportColType.DEFAULT } },
      { header: "重要级别", width: 8, ext: { prop: "level", type: ExportColType.DEFAULT } },
      { header: "安装的软件", width: 30, ext: { prop: "soft", type: ExportColType.DEFAULT } },
      { header: "用户名", width: 12, ext: { prop: "account", type: ExportColType.DEFAULT } },
      { header: "密码", width: 12, ext: { prop: "pwd", type: ExportColType.DEFAULT } }
    ]
  };
};

// 导出数据
const downloadFile = async () => {
  ElMessageBox.confirm("确认导出数据?", "温馨提示", { type: "warning" }).then(() =>
    useDownload(
      apiService.commonExport,
      `服务器清单-${useDateFormat(new Date(), "YYYY-MM-DD").value}`,
      "",
      getExcelConfig(),
      false
    )
  );
};

// 打开 drawer(新增、查看、编辑)
const drawerRef = ref<InstanceType<typeof DetailDrawer> | null>(null);
const openDrawer = (title: string, row: Partial<Backlog> = {}) => {
  const params = {
    title,
    tbName: initVar.tbName,
    isView: title === "查看",
    row: row,
    api: title.indexOf("新增") !== -1 ? apiService.save : title === "编辑" ? apiService.updateById : undefined,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};
</script>

<style scoped lang="scss">
:deep(.el-dialog__body) {
  height: 75vh !important;
  overflow: auto;
}
</style>
